package com.loveyukari.dao.impl;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import org.springframework.jdbc.core.RowMapper;
import org.springframework.stereotype.Repository;

import com.loveyukari.dao.LiwuStyleDao;
import com.loveyukari.model.LiwuStyle;

/**
*
*yangshaohua
*2016年12月13日下午6:01:17
**/

@Repository
public class JdbcLiwuStyleDao extends BaseDao implements LiwuStyleDao{

	public void insertLiwuStyle(LiwuStyle liwuStyle) {
		String sql = "insert into liwu_style (gfid,name,style,bimg,himg,grgb,brgb,urgb,drgb,pc,hit_interval,pc_full_icon,pc_full_bg_icon,cimg) values (?,?,?,?,?,?,?,?,?,?,?,?,?,?)";
		List<Object> params = new ArrayList<Object>();
		params.add(liwuStyle.getGfid());
		params.add(liwuStyle.getName());
		params.add(liwuStyle.getStyle());
		params.add(liwuStyle.getBimg());
		params.add(liwuStyle.getHimg());
		params.add(liwuStyle.getGrgb());
		params.add(liwuStyle.getBrgb());
		params.add(liwuStyle.getUrgb());
		params.add(liwuStyle.getDrgb());
		params.add(liwuStyle.getPc());
		params.add(liwuStyle.getHitInterval());
		params.add(liwuStyle.getPcFullIcon());
		params.add(liwuStyle.getPcFullBgIcon());
		params.add(liwuStyle.getCimg());
		this.getJdbcTemplate().update(sql, params.toArray());
		
	}

	@Override
	public void updateLiwuStyle(LiwuStyle liwuStyle) {
		String sql = "update liwu_style set name=?, style=?, bimg=?, himg=?, grgb=?, brgb=?, urgb=?, drgb=?, hit_interval=?, pc=?,pc_full_icon=?,pc_full_bg_icon=?,cimg=? where gfid = ?";
		this.getJdbcTemplate().update(sql, liwuStyle.getName(),liwuStyle.getStyle(),liwuStyle.getBimg(),liwuStyle.getHimg(),
				liwuStyle.getGrgb(),liwuStyle.getBrgb(),liwuStyle.getUrgb(),liwuStyle.getDrgb(),liwuStyle.getHitInterval(),liwuStyle.getPc(),liwuStyle.getPcFullIcon(),liwuStyle.getPcFullBgIcon(),liwuStyle.getCimg(),liwuStyle.getGfid());
	}


	public LiwuStyle selectLiwuStyleByGfid(Integer gfid) {
		String sql = "select * from liwu_style where gfid = ?";
		List<LiwuStyle> list = this.getJdbcTemplate().query(sql, new LiwuStyleRowMapper(),gfid);
		if (list.isEmpty()){
			return null;
		} else {
			return list.get(0);
		}
	}

	public List<LiwuStyle> selectLiwuStyleList() {
		String sql = "select * from liwu_style";
		return this.getJdbcTemplate().query(sql, new LiwuStyleRowMapper());
	}


	private class LiwuStyleRowMapper implements RowMapper<LiwuStyle>{

		public LiwuStyle mapRow(ResultSet rs, int rowNum) throws SQLException {
			LiwuStyle liwuStyle = new LiwuStyle();
			liwuStyle.setGfid(rs.getInt("gfid"));
			liwuStyle.setName(rs.getString("name"));
			liwuStyle.setUrgb(rs.getString("urgb"));
			liwuStyle.setBrgb(rs.getString("brgb"));
			liwuStyle.setDrgb(rs.getString("drgb"));
			liwuStyle.setGrgb(rs.getString("grgb"));
			liwuStyle.setBimg(rs.getString("bimg"));
			liwuStyle.setHimg(rs.getString("himg"));
			liwuStyle.setPc(rs.getInt("pc"));
			liwuStyle.setStyle(rs.getInt("style"));
			liwuStyle.setFree(rs.getInt("free"));
			liwuStyle.setHitInterval(rs.getInt("hit_interval"));
			liwuStyle.setPcFullBgIcon(rs.getString("pc_full_bg_icon"));
			liwuStyle.setPcFullIcon(rs.getString("pc_full_icon"));
			liwuStyle.setCimg(rs.getString("cimg"));
			return liwuStyle;
		}
		
	}

}
